package com.xinshujia.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinshujia.dao.FunctionDao;
import com.xinshujia.entity.FunctionEntity;
import com.xinshujia.entity.ThresholdEntity;
import com.xinshujia.service.FunctionService;
import com.xinshujia.utils.FunctionConstants;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * *************************************
 * *   @author: 野狼上校               **
 * *   @since: 2024-08-07 09:26       **
 * *   @version: 1.0                  **
 * *************************************
 * <p>
 * 与硬件交互的业务实体类
 */
@Service
public class FunctionServiceImpl extends ServiceImpl<FunctionDao, FunctionEntity> implements FunctionService {
    private final FunctionDao functionDao;

    public FunctionServiceImpl(FunctionDao functionDao) {
        this.functionDao = functionDao;
    }

    /**
     * (关闭/开启)采集
     * (开启/关闭/清除)报警
     * 查询阈值
     *
     * @param deviceGuid   设备Id
     * @param functionJson 命令 （Json字符串）
     */
    @Override
    public Boolean functionInsert(String deviceGuid, String functionJson) {
        FunctionEntity functionEntity = new FunctionEntity();
        functionEntity.setDeviceGuid(deviceGuid);
        functionEntity.setFunctionCommand(functionJson);
        functionEntity.setFunctionResult("");
        functionEntity.setFunctionExecuted(0);
        functionEntity.setCreaetTime(LocalDateTime.now());
        return functionDao.insert(functionEntity) > 0;
    }

    /**
     * 修改阈值
     *
     * @param deviceGuid   设备Id
     * @param functionJson 命令 （Json字符串）
     * @param threshold    新的阈值
     */
    @Override
    public Boolean functionInsert(String deviceGuid, String functionJson, Double threshold) {
        FunctionEntity functionEntity = new FunctionEntity();
        functionEntity.setDeviceGuid(deviceGuid);
        functionEntity.setFunctionCommand(functionJson + threshold + "}");
        functionEntity.setFunctionResult("");
        functionEntity.setFunctionExecuted(0);
        functionEntity.setCreaetTime(LocalDateTime.now());
        return functionDao.insert(functionEntity) > 0;
    }

    /**
     * 关闭/开启采集
     * </p>
     *
     * @param deviceGuid  设备GUID
     * @param operateType 操作类型 (0: 关闭采集， 1: 开启采集)
     */
    @Override
    public Boolean collectOperate(String deviceGuid, Integer operateType) {
        return switch (operateType) {
            // 关闭采集
            case 0 -> functionInsert(deviceGuid, FunctionConstants.FUNCTION_CLOSE_COLLECT);
            // 开启采集
            case 1 -> functionInsert(deviceGuid, FunctionConstants.FUNCTION_OPEN_COLLECT);
            default -> false;
        };
    }

    /**
     * 关闭/开启/清除报警
     *
     * @param deviceGuid  设备GUID
     * @param operateType 操作类型 (0: 关闭报警， 1: 开启报警, 2: 清除报警)
     */
    @Transactional
    @Override
    public Boolean alarmOperate(String deviceGuid, Integer operateType) {
        if (operateType.equals(2)) { // 清除报警
            return functionInsert(deviceGuid, FunctionConstants.FUNCTION_CLEAR_ALARM);
        } else if (operateType.equals(1)) { // 开启报警
            // 1. 关闭采集
            boolean open1 = collectOperate(deviceGuid, 0);
            // 2. 开启报警
            boolean open2 = functionInsert(deviceGuid, FunctionConstants.FUNCTION_OPEN_ALARM_X);
            boolean open3 = functionInsert(deviceGuid, FunctionConstants.FUNCTION_OPEN_ALARM_Y);
            boolean open4 = functionInsert(deviceGuid, FunctionConstants.FUNCTION_OPEN_ALARM_Z);
            // 3. 开启采集
            boolean open5 = collectOperate(deviceGuid, 1);
            return open1 && open2 && open3 && open4 && open5;
        } else if (operateType.equals(0)) { // 关闭报警
            // 1. 关闭采集
            boolean close1 = collectOperate(deviceGuid, 0);
            // 2. 关闭报警
            boolean close2 = functionInsert(deviceGuid, FunctionConstants.FUNCTION_CLOSE_ALARM_X);
            boolean close3 = functionInsert(deviceGuid, FunctionConstants.FUNCTION_CLOSE_ALARM_Y);
            boolean close4 = functionInsert(deviceGuid, FunctionConstants.FUNCTION_CLOSE_ALARM_Z);
            // 3. 开启采集
            boolean close5 = collectOperate(deviceGuid, 1);
            return close1 && close2 && close3 && close4 && close5;
        } else {
            return false;
        }
    }

    /**
     * 修改阈值
     */
    @Transactional
    @Override
    public Boolean updateThreshold(ThresholdEntity thresholdEntity) {
        String deviceGuid = thresholdEntity.getDeviceGuid();
        // 关闭采集
//        if (!collectOperate(deviceGuid, 0)) {
//            return false;
//        }

        // 关闭采集
        boolean update1 = collectOperate(deviceGuid, 0);
        // 执行修改阈值
        boolean update2 = functionInsert(deviceGuid, FunctionConstants.FUNCTION_UPDATE_THRESHOLD_X, thresholdEntity.getxAlarm());
        boolean update3 = functionInsert(deviceGuid, FunctionConstants.FUNCTION_UPDATE_THRESHOLD_Y, thresholdEntity.getyAlarm());
        boolean update4 = functionInsert(deviceGuid, FunctionConstants.FUNCTION_UPDATE_THRESHOLD_Z, thresholdEntity.getzAlarm());
        // 开启采集
        boolean update5 = collectOperate(deviceGuid, 1);
        return update1 && update2 && update3 && update4 && update5;
    }

}
